package com.cn.controller;

import com.cn.pojo.*;
import com.cn.service.GoodsService;
import com.cn.utils.uploadImage;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:王宇航
 * @create: 2023-05-20 22:59
 * @Description:
 */
@RestController //该Controller全部返回JSON格式
@RequestMapping("/goods")
@EnableWebMvc
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping(value = "/carousel", method = RequestMethod.POST)
    public List<Carousel> carousel() {
        return goodsService.carousel();
    }
    @RequestMapping(value = "/NewProductLaunch/{number}", method = RequestMethod.POST)
    public List<IndexGoods> NewProductLaunch(@PathVariable int number) {
        return goodsService.NewProductLaunch(number);
    }

    @RequestMapping(value = "/searchGoodsDetail/{id}", method = RequestMethod.POST)
    public Goods searchGoodsDetail(@PathVariable int id) {
        return goodsService.searchGoodsDetail(id);
    }
    @RequestMapping(value = "/addToCart/{userId}/{goodsId}", method = RequestMethod.POST)
    public boolean addToCart(@PathVariable int userId,@PathVariable int goodsId) {
        int i = goodsService.addToCart(userId, goodsId);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping(value = "/searchCartInfo/{userId}", method = RequestMethod.POST)
    public List<ShoppingCartItem> searchCartInfo(@PathVariable int userId) {
        List<ShoppingCartItem> shoppingCartItems = goodsService.searchCartInfo(userId);
        return shoppingCartItems;
    }

    @RequestMapping(value = "/deleCart/{goodsId}", method = RequestMethod.POST)
    public boolean deleCart(@PathVariable int goodsId) {
        int i = goodsService.deleCart(goodsId);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/updateCartNum/{num}/{goodsId}", method = RequestMethod.POST)
    public boolean updateCartNum(@PathVariable int num,@PathVariable int goodsId) {
        int i = goodsService.updateCartNum(num,goodsId);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/searchGoods/{info}", method = RequestMethod.POST)
    public List<Goods> searchGoods(@PathVariable String info) {
        List<Goods> goods = goodsService.searchGoods(info);
        if (goods!=null){
        return goods;
        }else
            return null;
    }
    @RequestMapping(value = "/searchCategory", method = RequestMethod.POST)
    public List<Category> searchCategory() {
        List<Category> categories = goodsService.searchCategory();
        return categories;
    }
    @RequestMapping(value = "/searchCategory2", method = RequestMethod.POST)
    public List<Category> searchCategory2() {
        List<Category> categories = goodsService.searchCategory2();
        return categories;
    }
    @RequestMapping(value = "/searchGoodsByCategory/{categoryId}", method = RequestMethod.POST)
    public List<Goods> searchGoodsByCategory(@PathVariable int categoryId) {
        List<Goods> goods = goodsService.searchGoodsByCategory(categoryId);
        return goods;
    }

    @RequestMapping(value = "/searchOrderItem/{userId}", method = RequestMethod.POST)
    public List<OrderItem> searchOrderItem(@PathVariable int userId) {
        List<OrderItem> orderItems = goodsService.searchOrderItem(userId);
        return orderItems;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public List<Goods> findAll() {
        List<Goods> all = goodsService.findAll();
        return all;
    }
    @RequestMapping(value = "/updateStatus/{status}/{id}", method = RequestMethod.POST)
    public void updateStatus(@PathVariable int status,@PathVariable int id) {
       goodsService.updateStatus(status,id);
    }

    @RequestMapping(value = "/updateCarousel/{id}", method = RequestMethod.POST)
    public void updateCarousel(@PathVariable int id) {
        goodsService.updateCarousel(id);
    }

    /*上传图片*/
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(MultipartFile file) throws IOException {
        String upload = uploadImage.upload(file);
        System.out.println(upload);
        return upload;
    }
    @RequestMapping(value = "/upload2", method = RequestMethod.POST)
    public Object upload2(MultipartFile file) throws IOException {
        String upload = uploadImage.upload(file);
//        System.out.println(upload);
//        Object o=upload;
        // 假设您已经从数据库或其他存储中获取了上传图片的路径

// 创建一个包含图片 URL 的对象
        Map<String, String> responseObj = new HashMap<>();
        responseObj.put("data", upload);

// 使用 Jackson 来将对象转换为 JSON 字符串
        ObjectMapper mapper = new ObjectMapper();
        String jsonResponse = mapper.writeValueAsString(responseObj);

// 将 JSON 响应返回给前端
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(jsonResponse);



    }
    /*管理员添加轮播图*/
    @RequestMapping(value = "/findIndexConfig/{id}", method = RequestMethod.POST)
    public List<IndexGoods> findIndexConfig(@PathVariable int id) {
        return goodsService.findIndexConfig(id);
    }

    @RequestMapping(value = "/findCartItem/{userId}/{goodsId}", method = RequestMethod.POST)
    public boolean findCartItem(@PathVariable int userId,@PathVariable int goodsId) {
        ShoppingCartItem cartItem = goodsService.findCartItem(userId, goodsId);
       if (cartItem!=null){
           goodsService.addNum(Math.toIntExact(cartItem.getCartItemId()));
           return true;
       }else {
           return false;
       }
    }

    @RequestMapping(value = "/addCarousel", method = RequestMethod.POST)
    public boolean addCarousel(@RequestBody Carousel carousel) {
        int i = goodsService.addCarousel(carousel);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/opera", method = RequestMethod.POST)
    public boolean addGoods(@RequestBody Goods goods) {
        int i = goodsService.addGoods(goods);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @RequestMapping(value = "/opera", method = RequestMethod.PUT)
    public boolean editGoods(@RequestBody Goods goods) {
        int i = goodsService.editGoods(goods);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping(value = "/indexConfigs/{configId}", method = RequestMethod.DELETE)
    public boolean deleIndexConfigs(@PathVariable int configId) {
        int i = goodsService.deleIndexConfigs(configId);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping(value = "/findByConfigId/{configId}", method = RequestMethod.POST)
    public IndexGoods findByConfigId(@PathVariable int configId) {
        IndexGoods byConfigId = goodsService.findByConfigId(configId);
        return byConfigId;
    }
    @RequestMapping(value = "/addIndexConfigs", method = RequestMethod.POST)
    public String addIndexConfigs(@RequestBody IndexGoods indexGoods) {
        Goods goods = goodsService.searchGoodsDetail(Math.toIntExact(indexGoods.getGoodsId()));
        if (goods!=null){
            goodsService.addIndexGoods(indexGoods);
            return "添加成功";
        }else {
            return "没有该商品";
        }

    }
    @RequestMapping(value = "/updateIndexConfigs", method = RequestMethod.POST)
    public void updateIndexConfigs(@RequestBody IndexGoods indexGoods) {
        goodsService.updateIndexGoods(indexGoods);
    }
    @RequestMapping(value = "/findCategory/{categoryId}", method = RequestMethod.POST)
    public Category findCategory(@PathVariable int categoryId) {
        Category category = goodsService.findCategory(categoryId);
        return category;
    }
    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public void addCategory(@RequestBody Category category) {
        goodsService.addCategory(category);
    }
    @RequestMapping(value = "/editCategory", method = RequestMethod.POST)
    public void editCategory(@RequestBody Category category) {
        goodsService.editCategory(category);
    }
    @RequestMapping(value = "/deleCategory/{categoryId}", method = RequestMethod.POST)
    public void deleCategory(@PathVariable int categoryId) {
        goodsService.deleCategory(categoryId);
    }
}
